package com.yhh;

import com.yhh.Entity.Command;
import com.yhh.Entity.HandlerCommand;
import com.yhh.Exceptions.InstructionException;
import com.yhh.Exceptions.NonInstructionException;

import java.io.IOException;
import java.net.Socket;
import java.util.Map;

public class ShowCommand extends Command implements HandlerCommand {
    protected ShowCommand(Socket socket) {
        super(socket);
        command.add("-a");
    }

    @Override
    public void handlerParameter(Map<String, String> args) throws InstructionException, NonInstructionException {

    }

    @Override
    public boolean check(Map<String, String> args) throws NonInstructionException, InstructionException {
        return true;
    }

    @Override
    public void handlerCommand(Map<String, String> args) throws InstructionException, NonInstructionException {
        String resp = recvMsg();
        if(resp.startsWith("counts:")){
            handlerShow(resp);
        }else {
            System.out.println(resp);
        }
    }
    private void handlerShow(String info) {
        String[] split = info.split(":");
        int counts = Integer.parseInt(split[1]);
        if (counts <= 0) {
            System.out.println("没有文件");
        } else {
            while (counts-- != 0) {
                String msg = recvMsg();
                System.out.println(msg);
            }
        }
    }
}
